package com.yxkj.syh.app.huarong.activities.creat.bank;

import android.content.Intent;
import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.syh.app.basic.base.BaseActivity;
import com.yxkj.syh.app.huarong.BR;
import com.yxkj.syh.app.huarong.R;
import com.yxkj.syh.app.huarong.adps.PaymentsAdp;
import com.yxkj.syh.app.huarong.bean.Payment;
import com.yxkj.syh.app.huarong.constants.ArouterPath;
import com.yxkj.syh.app.huarong.databinding.ActivityBankCardsBinding;

/**
 * 收款信息
 */
@Route(path = ArouterPath.BANK_CARDS_ACTIVITY)
public class BankCardsActivity extends BaseActivity<ActivityBankCardsBinding, BankCardsVM> {

    @Autowired
    boolean isSelect = false;
    @Autowired
    boolean isFromOrder = false;
    @Autowired
    long id;

    private PaymentsAdp mPaymentsAdp;

    @Override
    protected int getContentLayout() {
        return R.layout.activity_bank_cards;
    }

    @Override
    protected int getViewModelId() {
        return BR.bankCardsVM;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {


        mVDBinding.titleView.setLeftOnClickListener(v -> {
            finish();
        });

        mVDBinding.rvBankCards.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        mPaymentsAdp = new PaymentsAdp();
        mPaymentsAdp.setOnViewClick(new PaymentsAdp.OnViewClick() {
            @Override
            public void onEdit(Payment payment) {
                ARouter.getInstance().build(ArouterPath.EDIT_BANK_CARDS_ACTIVITY).withSerializable("payment", payment).withLong("linkmanID", id).navigation();
            }

            @Override
            public void onItem(Payment payment) {
                if (isSelect) {
                    Intent resultIntent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("data", payment);
                    resultIntent.putExtras(bundle);
                    BankCardsActivity.this.setResult(RESULT_OK, resultIntent);
                    BankCardsActivity.this.finish();
                }
            }
        });
        mPaymentsAdp.bindToRecycleView(mVDBinding.rvBankCards);

        if (isSelect&&isFromOrder) {
            mPaymentsAdp.setEditAble(false);
        }else {
            mPaymentsAdp.setEditAble(true);
            mVDBinding.titleView.setRightText("添加收款方式");
        }

        mVDBinding.titleView.setRightOnClickListener(v -> {
            ARouter.getInstance().build(ArouterPath.EDIT_BANK_CARDS_ACTIVITY).withLong("linkmanID", id).navigation();
        });

        mVDBinding.srlBankCards.setOnRefreshListener(refreshLayout -> {
            mViewModel.getPaymentList(id);
        });

    }

    @Override
    protected void initData() {
        mViewModel.setLinkmanID(id);
    }

    @Override
    protected void initObservers() {
        super.initObservers();
        mViewModel.mldPayments.observe(this, payments -> {
            mPaymentsAdp.setNewData(payments);
            if (mVDBinding.srlBankCards.isRefreshing()) {
                mVDBinding.srlBankCards.finishRefresh();
            }
        });
    }


}
